package com.hzsk.lisence.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzsk.lisence.config.AnnotationForLog;
import com.hzsk.lisence.entity.LogEnum;
import com.hzsk.lisence.entity.SoftwareEntity;
import com.hzsk.lisence.entity.SoftwareSonEntity;
import com.hzsk.lisence.entity.query.SelectByNameQuery;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.mapper.SoftwareLicenseMapper;
import com.hzsk.lisence.mapper.SoftwareMapper;
import com.hzsk.lisence.mapper.SoftwareSonMapper;
import com.hzsk.lisence.mapper.UserPermissionMapper;
import com.hzsk.lisence.service.SoftwareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class SoftwareServiceImpl implements SoftwareService {

    @Resource
    private UserPermissionMapper userPermissionMapper;
    @Resource
    private SoftwareMapper softwareMapper;

    @Resource
    private SoftwareSonMapper softwareSonMapper;

    @Resource
    private SoftwareLicenseMapper softwareLicenseMapper;

    @Override
    public Object getSoftwareListSelected(SelectByNameQuery selectByNameQuery) {
        PageHelper.startPage(selectByNameQuery.getPageNum(), selectByNameQuery.getPageSize());
        List<SoftwareEntity> infos = softwareMapper.getSoftwareInfoSelected(selectByNameQuery);
        for (SoftwareEntity info : infos) {
            Integer id = info.getId();
            List<SoftwareSonEntity> softwareListSonInfo = softwareSonMapper.getSoftwareListSonInfo(null,id,1);
            info.setSoftwareSons(softwareListSonInfo);
            Integer integer = softwareLicenseMapper.statisticsBySoftwareId(id);
            info.setLisenceNum(integer);
        }
        return new PageInfo<>(infos);
    }

    @Override
    public Object getAllSoftware(Integer operatorUserId) {
        return softwareMapper.getAllSoftware(operatorUserId);
    }

    @Override
    @AnnotationForLog(operationType = LogEnum.CREATE_SOFTWARE,operation = "创建软件信息",operationalEntity = "SoftwareEntity.java/STUDENT_T")
    public void saveSoftwareInf(SoftwareEntity softwareEntity, Integer userId, IncResponce incResponce) {
        String logo = softwareEntity.getSoftwareLogo();
        String name = softwareEntity.getSoftwareName();
        String describe = softwareEntity.getSoftwareDescribe();
        if(logo.isEmpty() || name.isEmpty() || describe.isEmpty()){
            incResponce.setCode(1);
            incResponce.setErrorMessage("请求参数异常");
        } else {
            List<SoftwareEntity> softwareInfoByName = softwareMapper.getSoftwareInfoByName(name);
            if (softwareInfoByName.size() > 0){
                incResponce.setCode(1);
                incResponce.setErrorMessage("软件名称不能为空");
                return;
            }
            softwareMapper.saveSoftwareInf(softwareEntity);
            int softwareId = softwareMapper.getSoftwareId(name);
            userPermissionMapper.saveUserPermission(userId,softwareId);
            incResponce.setCode(0);
            incResponce.setErrorMessage("操作成功");
        }
    }



    @Override
    @AnnotationForLog(operationType = LogEnum.UPDATE_SOFTWARE,operation = "修改软件信息",operationalEntity = "SoftwareEntity.java/STUDENT_T")
    public void updateSoftwareInf(SoftwareEntity softwareEntity,Integer userId,IncResponce incResponce) {
        String name = softwareEntity.getSoftwareName();
        List<SoftwareEntity> softwareInfoByName = softwareMapper.getSoftwareInfoByName(name);
        if (softwareInfoByName.size() >= 1  && !Objects.equals(softwareInfoByName.get(0).getId(), softwareEntity.getId())){
            incResponce.setCode(1);
            incResponce.setErrorMessage("软件名称不能为空");
            return;
        }
        softwareMapper.updateSoftwareInf(softwareEntity);
        incResponce.setCode(0);
        incResponce.setErrorMessage("操作成功");
    }
    @Override
    @AnnotationForLog(operationType = LogEnum.DEL_SOFTWARE,operation = "删除软件信息",operationalEntity = "SoftwareEntity.java/STUDENT_T")
    public String deleteSoftwareInf(Integer softwareId,Integer userId) {
        softwareMapper.deleteSoftwareInf(softwareId);
        userPermissionMapper.deleteUserPermission(softwareId);
        return "操作成功";
    }
}
